package com.xiaoshuidi.cloud.module.finance.convert.finance;

import com.xiaoshuidi.cloud.module.finance.dal.dataobject.finance.FinanceThirdWithdrawDO;
import com.xiaoshuidi.cloud.module.finance.job.withdraw.dto.TongLianWithdrawDTO;
import lombok.extern.slf4j.Slf4j;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

@Mapper
public interface TongLianConvert {
    Logger logger = LoggerFactory.getLogger(TongLianConvert.class);

    TongLianConvert INSTANCE = Mappers.getMapper(TongLianConvert.class);

    @Mapping(target = "balance", source = "clearamt", qualifiedByName = "mapStringToLong")
    @Mapping(target = "receiveDate", source = "clearsplittime", qualifiedByName = "mapStringToLocalDate")
    @Mapping(target = "reason", source = "errorreason")
    @Mapping(target = "withdrawTime", source = "expectclearday", qualifiedByName = "mapStringToLocalDateTime")
    @Mapping(target = "fee", source = "fee", qualifiedByName = "mapStringToLong")
    @Mapping(target = "status", source = "iscleared")
    @Mapping(target = "accountName", source = "acctname")
    @Mapping(target = "bankName", source = "bankname")
    @Mapping(target = "accountNo", source = "acctno")
    @Mapping(target = "apartmentId", source = "apartmentId")
    FinanceThirdWithdrawDO convert(TongLianWithdrawDTO reqDTO);


    @Named("mapStringToLong")
    default Long mapStringToLong(String value) {
        if (value == null || value.isEmpty()) {
            return null; // 或者返回默认值
        }
        try {
            return Long.valueOf(value); // 将字符串转换为 Long
        } catch (NumberFormatException e) {
            // 处理转换异常
            logger.error("通联对象转换异常，String转Long失败",e);
            return null; // 或者可以抛出 RuntimeException
        }
    }

    @Named("mapStringToLocalDate")
    default LocalDate mapStringToLocalDate(String dateStr) {
        if (dateStr == null || dateStr.isEmpty()) {
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        try {
            return LocalDate.parse(dateStr, formatter);
        } catch (DateTimeParseException e) {
            // 处理转换异常
            logger.error("通联对象转换异常，String转LocalDate失败",e);
            return null;
        }
    }

    @Named("mapStringToLocalDateTime")
    default LocalDateTime mapStringToLocalDateTime(String dateStr) {
        if (dateStr == null || dateStr.isEmpty()) {
            return null;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmm");
        try {
            return LocalDateTime.parse(dateStr, formatter);
        } catch (DateTimeParseException e) {
            // 处理转换异常
            logger.error("通联对象转换异常，String转LocalDateTime失败",e);
            return null;
        }
    }
}
